package org.red_cross_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.red_cross_service.domain.Protocol;
import org.red_cross_service.mapper.ProtocolMapper;
import org.red_cross_service.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProtocolService {

    @Autowired
    private ProtocolMapper protocolMapper;

    @Autowired
    private RedisUtil redisUtil;

    public String getProtocol() {
        if(redisUtil.hasKey("protocol")){

             return redisUtil.getCacheObject("protocol");
        }
        else {
            Protocol protocol = protocolMapper.selectOne(new QueryWrapper<Protocol>(new Protocol().setProtocolId(1)));
            redisUtil.setCacheObject("protocol", protocol.getContent());
            return protocol.getContent();
        }
    }

    public int updateProtocol(Protocol protocol) {
        protocolMapper.updateById(protocol);
        redisUtil.setCacheObject("protocol",protocol.getContent());
        return 1;
    }

}
